// Q2. Given 3 integers A, B, C, they represent the count of the
// letters 'a', 'b', and 'c'. Return any string that contains A as,
// B bs, and C cs, subject to the condition that it cannot contain 3
// consecutive as, bs, or cs. For example A=1,B=2,C=3; you may return abccbc.

// greedy
package main

import (
	"fmt"
	"sort"
)

type WordCount struct {
	letter string
	count  int
}

func formatString(input map[string]int) string {
	res := ""
	total := 0
	aa := []WordCount{}
	for key, value := range input {
		total += value
		wc := WordCount{
			letter: key,
			count:  value,
		}
		aa = append(aa, wc)
	}
	sort.Slice(aa, func(i, j int) bool {
		return aa[i].count <= aa[j].count
	})
	// fmt.Println(aa)
	i := 0
	j := len(aa) - 1
	cnt := 0
	for len(res) != total {

		res += aa[j].letter
		aa[j].count--
		cnt++
		if aa[j].count == 0 {
			j--
			cnt = 0
		}
		if cnt == 2 && aa[j].count != 0 {
			res += aa[i].letter
			aa[i].count--
			cnt = 0
			if aa[i].count == 0 {
				i++
			}
		}

	}
	return res
}
func main() {

	input := map[string]int{
		"A": 1,
		"B": 2,
		"C": 4,
	}
	fmt.Println(formatString(input))
}
